package test.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import test.mp.domain.dto.PageDTO;
import test.mp.domain.po.User;
import test.mp.domain.dto.UserFormDTO;
import test.mp.domain.query.UserQuery;
import test.mp.domain.vo.UserVO;
import test.mp.service.IUserService;

import java.util.List;

/**
 * @ClassName UserController
 * @Description 用户请求controller
 * @Author wxx
 * @Date 2024/08/15/0:04
 * @Version 1.0
 */
@Api(tags = "用户管理接口")
@RequestMapping("/users")
@RestController
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;


    @ApiOperation("新增用户接口")
    @PostMapping("/save")
    public void saveUser(@RequestBody UserFormDTO userDTO){
        // 1. 把DTO拷贝到PO
        User user = BeanUtil.copyProperties(userDTO, User.class);
        // 2. 新增
        userService.save(user);
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("{id}")
    public void deleterUserById(@ApiParam("用户id") @PathVariable("id") String id){
        userService.removeById(id);
    }

    @ApiOperation("根据id查询用户接口")
    @GetMapping("{id}")
    public UserVO quertUserById(@ApiParam("用户id") @PathVariable("id") String id){
      /* // 1. 查询用户PO
       User user = userService.getById(id);
       // 2. 把PO拷贝到DTO
        return BeanUtil.copyProperties(user, UserVO.class);*/

        return userService.queryUserAndAddressById(id);

    }


    @ApiOperation("根据ids批量查询用户接口")
    @GetMapping
    public List<UserVO> queryUserByIds(@ApiParam("用户id") @RequestParam List<String> ids){
        // 1. 根据ids查询PO
        // List<User> users = userService.listByIds(ids);
        // 2. 把PO拷贝到DTO
        //return BeanUtil.copyToList(users, UserVO.class);

        return userService.queryUserAndAddressByIds(ids);
    }

    @ApiOperation("扣除用户余额接口")
    @PutMapping("{id}/deduction/{money}")
    public void deductBalance(@ApiParam("用户id") @PathVariable("id") String id,
                              @ApiParam("扣减的金额") @PathVariable("money") Integer money){
        userService.deductBalance(id, money);
    }


    @ApiOperation("根据复杂条件查询用户接口")
    @GetMapping("/queryList")
    public List<UserVO> queryUsers(UserQuery userQuery){
        // 1.查询用户id
        List<User> users = userService.queryUsers(userQuery);
        // 2.把PO拷贝到VO
        return BeanUtil.copyToList(users, UserVO.class);
    }


    @ApiOperation("根据分页条件查询用户接口")
    @GetMapping("/queryUsersPage")
    public PageDTO<UserVO> queryUsersPage(UserQuery userQuery){
        return userService.queryUsersPage(userQuery);
    }


}
